-- 运营活动:首充返利


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 67, -- 活动id (67 首充返利)
    act_name = "首充返利", -- 活动名称
    act_desc = "首充返利而已啦", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1693267400,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        precent = 30, --百分数 
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_67_check_cfg(cfg)
    if cfg == nil then return 20 end
    
    local config = cfg.config
    if config == nil then return 21 end
    

    return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_67_setup(cfg) 
    return 0
end

-- 卸载活动
function oper_act_67_unsetup(cfg)   
end

-- 活动开始
function oper_act_67_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_67_start = 1
    ssVar.oper_act_67_cmd = cfg.cmd_id
    ssVar.oper_act_67_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_67, 1)

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            return
        end
    end

    gRankMgr_Clear(RANK_DEFINE_OPERATION_67)
end

-- 活动结束
function oper_act_67_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_67_start = nil
    ssVar.oper_act_67_cmd = nil
    ssVar.oper_act_67_act = nil

    if not cfg then return end
    local config = cfg.config
    if not config then return end

end

-- 打包数据
function oper_act_67_packet_data(sysarg, cfg)

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_67_start then
        return
    end
    if not cfg then return end
    local config = cfg.config
    if not config then return end

end

-- 操作，返回操作结果，0为成功
function oper_act_67_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_67_do cfg.config confg error")
        return
    end
end

--[[
-- 跨天
function oper_act_67_new_day_arrive(act_id) 
    
end
]]


function oper_act_67_withdrawyuanbao(sysarg, nYuanBao)
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_67_start then
        return
    end
    if not ssVar.oper_act_67_cmd then
        return
    end
    if not ssVar.oper_act_67_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_67_cmd, ssVar.oper_act_67_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then
        return
    end
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_67, 1)

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end

    --[[
    if not myData.myRechargeData then
        myData.myRechargeData = {}
    end
    local len = #myData.myRechargeData
    for idx = 1, len do
        if myData.myRechargeData[idx] and myData.myRechargeData[idx] == nYuanBao then
            return
        end
    end
    table.insert(myData.myRechargeData, nYuanBao)

    ]]

    if myData.myRechargeData then
        return
    end

    myData.myRechargeData = 1
    local time = System.getSystemTime()
    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end


    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_67, 1, newData)

    local awardNum = math.floor(config.precent * nYuanBao / 100)
    local awardType = config.awardType
    local rewards = {{ type = awardType, count = awardNum, bind = 0},}

    local title = string.format(Lang.ScriptTips.Operation202, cfg.act_name)
    local content = string.format(Lang.ScriptTips.Operation203, cfg.act_name)
    local actorId = Actor.getActorId(sysarg)
    Awards.GiveToMailById(actorId, rewards, title, content, LogIds.OperationLogId067, Lang.LogTips.Operation067)
end